﻿using System;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using DXTools.UI.Core;
using DXTools.UI.Modules.Statistics.Views;
using DXTools.UI.Core.Events;
using DXTools.UI.Core.ViewModel;

namespace DXTools.UI.Modules.Statistics.ViewModels
{
    public class ToolbarViewModel : ViewModelBase
    {

        public DelegateCommand<object> ViewEntitiesCommand { get; private set; }
        public DelegateCommand<object> ExportToExcelCommand { get; private set; }

        public ToolbarViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer container)
            : base(regionManager, eventAggregator, container)
        {
            this.HeaderInfo = "Statistics";
            this.ViewEntitiesCommand = new DelegateCommand<object>(this.ViewEntities, this.CanViewEntities);
            this.ExportToExcelCommand = new DelegateCommand<object>(this.ExportToExcel, this.CanExportToExcel);

            // View collection changed
            this.RegionManager.Regions["BodyRegion"].ActiveViews.CollectionChanged += (sender, e) =>
            {
                ExportToExcelCommand.RaiseCanExecuteChanged();
            };

        }

        private void ViewEntities(object commandArg)
        {
            // Single View
            string viewName = typeof(EntitiesView).FullName;
            IRegion bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            object view = bodyRegion.GetView(viewName);
            if (view == null)
            {
                EntitiesView newView = UnityContainer.Resolve<EntitiesView>();
                bodyRegion.Add(newView, viewName);
                bodyRegion.Activate(newView);
                EventAggregator.GetEvent<ViewAddedEvent>().Publish(newView);
            }
            else
                bodyRegion.Activate(view);
         
        }

        private bool CanViewEntities(object commandArg)
        {
            return true;
        }

        private void ExportToExcel(object commandArg)
        {
            var singleView = this.RegionManager.Regions["BodyRegion"].ActiveViews.FirstOrDefault();
            if (singleView != null)
            {
                DXTools.UI.Core.View.ViewBase view = ((DXTools.UI.Core.View.ViewBase)singleView);

                if (view.GetType() == typeof(EntitiesView))
                {
                    object grid = view.FindName("uxData");
                    if (grid != null)
                        ExportGridToExcel((DataGrid)grid);

                }

            }
           

        }

        private void ExportGridToExcel(DataGrid Grid)
        {
            if (Grid.ItemsSource != null)
            {
                DXTools.Core.DataGridExtensions.ExportDataGrid(Grid);
            }
            else
            {
                ChildWindow msg = new ChildWindow();
                msg.Content = "No data to export";
                msg.Show();
            }
        }

        private bool CanExportToExcel(object commandArg)
        {
            bool canExport = false;

            var singleView = this.RegionManager.Regions["BodyRegion"].ActiveViews.FirstOrDefault();
            if (singleView != null)
            {
                DXTools.UI.Core.View.ViewBase view = ((DXTools.UI.Core.View.ViewBase)singleView);

                if (view.GetType() == typeof(EntitiesView))
                {
                    canExport = true;
                }
            }
           
            return canExport;
            

        }

         
    }
}
